package com.qfjy.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qfjy.client.video.VideoInfoFeignClient;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.service.MeetingPubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Slf4j
@RequestMapping("meetingPub")
@DefaultProperties(defaultFallback = "defaultFallback")
public class MeetingPubController {

    @Autowired
    private MeetingPubService meetingPubService;

    @Autowired
    private VideoInfoFeignClient videoInfoFeignClient;

    /**
     * TODO 根据会议编号查询会议信息  meetingPub/select?pcode=1111
     * @return
     */
    @ResponseBody
    @GetMapping("select")
    public Object selectMeetingPubByPcode(@RequestParam("pcode")String pcode){

       MeetingPub meetingPub= meetingPubService.selectMeetingPubByPcode(pcode);
       log.info("查询的是会议微服务信息：{}",meetingPub);


        /**
         * 向在本地调用其它方法一样
         * Fiegn组件完成服务间通信
         */
        String videoInforesult= videoInfoFeignClient.selectVideoInfoByPcode(pcode);



       String select1= videoInfoFeignClient.selectVideoInfoByPcode1("123456");
       meetingPub.setPzone(select1);

        meetingPub.setRemark(videoInforesult);
       //要给的是一个ResultJson 统一内容格式（统一内容协议）
        return meetingPub;
    }

    @Autowired
    private RestTemplate restTemplate;



    @GetMapping("info")
    @ResponseBody
    public String info(@RequestParam("id") int id){
        if(id>1000){
            throw new RuntimeException("id大于1000");
        }
        return "info 正常返回id数据:"+id;
    }

    /**
     * 服务降级：当服务内部出现异常情况，将触发降级，这个降级是每次请求都会去触发，走默认处理方法defaultFallback
     * @HystrixCommand 注解
     *  1设置 fallbackMethod 指定的 返回值方法类型要跟目标方法一致，否则将报错。
     *  2如果方法内部有明显的异常,将不走目标方法，直接返回 fallback 方法的返回值。
     *
     * ###
     */
    @GetMapping("info1")
    @ResponseBody
    @HystrixCommand(fallbackMethod ="fallbackInfo1" )
    public String info1(@RequestParam("id") int id){
        if(id>1000){
            throw new RuntimeException("id大于1000");
        }
        return "info1 正常返回id数据:"+id;
    }

    public String fallbackInfo1( int id){
        return "info1降级的默认方法是："+id;
    }


    @GetMapping("info2")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "fallbackInfo2")
    public String info2(@RequestParam("id") int id){
        if(id>1000){
            throw new RuntimeException("id大于1000");
        }
        return "info2 正常返回id数据:"+id;
    }
    public String fallbackInfo2( int id){
        return "info2 降级默认方法是："+id;
    }

    @GetMapping("info3")
    @ResponseBody
    @HystrixCommand
    public String info3(@RequestParam("id") int id){
        if(id>1000){
            throw new RuntimeException("id大于1000");
        }
        return "info3 正常返回id数据:"+id;
    }

    @GetMapping("info4")
    @ResponseBody
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "6"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")
    })
    public String info4(@RequestParam("id") int id){
        log.info("走这个方法了--》"+id);
        if(id>1000){
            throw new RuntimeException("id大于1000");
        }
        return "info4 正常返回id数据:"+id;
    }
    public String defaultFallback(){
        return "服务器开小差了或者：网络繁忙，稍候再试etc..";
    }
}
